
import 'package:flutter/material.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class GiftCardConsumptionRecordLogic extends GetxController {
  final state = GiftCardConsumptionRecordState();
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  @override
  void onInit() {
    super.onInit();
    state.orderNumberQueryController = TextEditingController();
    state.orderNumberQueryFocusNode = FocusNode();

    getGiftCardConsumptionRecordList();
    state.orderNumberQueryController.addListener(() {
      state.currentPage = 1;
      getGiftCardConsumptionRecordList();
    });
  }

  /// 获取福利卡消费记录
  void getGiftCardConsumptionRecordList() {
    Map<String, dynamic> params = {
      "member_num_id": SpUtil.getInt('empeNumId'),
      "page_num": state.currentPage,
      "page_size": state.pageSize,
      "card_no": state.orderNumberQueryController.text,
      'trade_no': state.orderNumberQueryController.text
    };
    Api.getGiftCardConsumptionRecord(params).listen((event) {
      refreshController.loadComplete();
      refreshController.refreshCompleted();
      print("获取福利卡消费记录==========$event");
      if (event["code"] == 0) {
        List list = event["consume_details"];
        if (state.currentPage == 1) {
          if (list != null) {
            state.consumptionRecordList = list;
            setType(  state.consumptionRecordList );
          } else {
            state.consumptionRecordList = [];
            setType( state.consumptionRecordList );
          }
        } else {
          if (list != null) {
            state.consumptionRecordList.addAll(list);
                    setType(  state.consumptionRecordList );
          }
        }
        if ((list == null || list.isEmpty || list.length < state.pageSize) && state.consumptionRecordList.length > 0) {
          if (state.currentPage > 1) {
            refreshController.loadNoData();
          }
        }
        update();
      }
    });
  }

  setType(List list){
    list.forEach((element) {
      if(element['consume_type'] ==1){
        element['consume_typeText']='扣款';
      }
       if(element['consume_type'] ==2){
        element['consume_typeText']='退款';
      }
        if(element['consume_type'] ==3){
        element['consume_typeText']='取消支付';
      }

    });
  }


}